<!DOCTYPE html>
<html>
<head>
    <:include file="../common/css.html" title="QrtzJobs管理"/>
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">任务类名:</label>
                        <div class="layui-input-inline">
                            <input name="search@like@jobClassName" class="layui-input" placeholder="输入任务类名"/>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="QrtzJobsTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="QrtzJobsTable" lay-filter="QrtzJobsTable"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="QrtzJobsTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs"
       data-dropdown="#QrtzJobsTbDelDrop{{d.LAY_INDEX}}" no-shade="true">删除</a>
    <div class="dropdown-menu-nav dropdown-popconfirm dropdown-top-right layui-hide"
         id="QrtzJobsTbDelDrop{{d.LAY_INDEX}}"
         style="max-width: 200px;white-space: normal;min-width: auto;margin-left: 10px;">
        <div class="dropdown-anchor"></div>
        <div class="dropdown-popconfirm-title">
            <i class="layui-icon layui-icon-help"></i>
            确定要删除吗？
        </div>
        <div class="dropdown-popconfirm-btn">
            <a class="layui-btn" btn-cancel>取消</a>
            <a class="layui-btn layui-btn-normal" lay-event="del">确定</a>
        </div>
    </div>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="statusState">
    <input type="checkbox" lay-filter="statusStateCk" value="{{d.id}}" lay-skin="switch"
           lay-text="正常|停止" {{d.status==0?'checked':''}}/>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="QrtzJobsEditDialog">
    <form id="QrtzJobsEditForm" lay-filter="QrtzJobsEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">类名:</label>
            <div class="layui-input-block">
                <input name="jobClassName" placeholder="请输入类名或Bean名" class="layui-input"
                       lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">方法名:</label>
            <div class="layui-input-block">
                <input name="method" placeholder="请输入方法名" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">表达式:</label>
            <div class="layui-input-block">
                <input name="cronExpression" placeholder="请输入cron表达式" class="layui-input"
                       lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">参数:</label>
            <div class="layui-input-block">
                <input name="parameter" placeholder="请输入参数" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">描述:</label>
            <div class="layui-input-block">
                <input name="description" placeholder="请输入描述" class="layui-input"
                       lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="QrtzJobsEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<:include file="../common/js.html"/>
<script>
    layui.use(['layer', 'laydate', 'form', 'table', 'tableX', 'util', 'admin', 'dropdown'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var form = layui.form;
        var table = layui.table;
        var tableX = layui.tableX;
        var util = layui.util;
        var admin = layui.admin;

        /* 渲染表格 */
        var insTb = tableX.render({
            elem: '#QrtzJobsTable',
            url: '${ctxPath}/sys/QrtzJobs/page',
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {field: 'jobClassName', title: '任务类名', sort: true},
                {field: 'method', title: '方法', sort: true},
                {field: 'cronExpression', title: 'cron表达式', sort: true},
                {field: 'parameter', title: '参数', sort: true},
                {field: 'description', title: '描述', sort: true},
                {field: 'status', title: '状态', templet: '#statusState', sort: true},
                {title: '操作', toolbar: '#QrtzJobsTbBar', align: 'center', width: 200, minWidth: 200}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(QrtzJobsTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(QrtzJobsTable)', function (obj) {
            if ('edit' === obj.event) { // 修改
                showEditModel(obj.data);
            } else if ('del' === obj.event) { // 删除
                doDel(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(QrtzJobsTable)', function (obj) {
            if ('add' === obj.event) { // 添加
                showEditModel();
            } else if ('del' === obj.event) { // 删除
                var checkRows = table.checkStatus('QrtzJobsTable');
                if (0 === checkRows.data.length) {
                    return layer.msg('请选择要删除的数据', {icon: 2, anim: 6});
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        form.on('switch(statusStateCk)', function (obj) {
            var loadIndex = layer.load(2);
            $.post('${ctxPath}/sys/QrtzJobs/edit', {
                id: obj.elem.value,
                status: obj.elem.checked ? 0 : 1,
                statusEdit: 0
            }, function (res) {
                layer.close(loadIndex);
                if (0 === res.code) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2, anim: 6});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox', $('#QrtzJobsTable').next().attr('lay-filter'));
                }
            });
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '定时任务',
                content: $('#QrtzJobsEditDialog').html(),
                area:['700px', '500px'],
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('QrtzJobsEditForm', mData);
                    // 表单提交事件
                    form.on('submit(QrtzJobsEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        $.post(mData ? '${ctxPath}/sys/QrtzJobs/edit' : '${ctxPath}/sys/QrtzJobs/save', data.field, function (res) {
                            layer.close(loadIndex);
                            if (0 === res.code) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2, anim: 6});
                            }
                        });
                        return false;
                    });
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            if (obj.ids) {  // 批量删除
                admin.confirm('确定要删除选中数据吗？', function (i) {
                    layer.close(i);
                    var loadIndex = layer.load(2);
                    admin.req('${ctxPath}/sys/QrtzJobs/removeBatch', JSON.stringify(obj.ids), function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            layer.msg(res.msg, {icon: 1});
                            insTb.reload({page: {curr: 1}});
                        } else {
                            layer.msg(res.msg, {icon: 2, anim: 6});
                        }
                    }, 'post');
                });
            } else {  // 单个删除
                var loadIndex = layer.load(2);
                $.get('${ctxPath}/sys/QrtzJobs/remove', {id: obj.data.id}, function (res) {
                    layer.close(loadIndex);
                    if (0 === res.code) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2, anim: 6});
                    }
                });
            }
        }

    });
</script>
</body>
</html>
